package com.nbjtjc.safe.api.emg;

import cn.hutool.core.collection.CollectionUtil;
import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.api.AbstractController;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.emg.EmgPlanPublicGoodsHistoryMapper;
import com.nbjtjc.safe.model.EmgPlanPublicGoodsHistory;
import com.nbjtjc.safe.model.emg.EmgPlanPublicGoods;
import com.nbjtjc.safe.service.emg.EmgPlanPublicGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 发布的应急预案关联的物资
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/planPublicGoods")
public class EmgPlanPublicGoodsController extends AbstractController<EmgPlanPublicGoods> {
    @Autowired
    private EmgPlanPublicGoodsService emgPlanPublicGoodsService;

    @Autowired
    private EmgPlanPublicGoodsHistoryMapper emgPlanPublicGoodsHistoryMapper;

    @Override
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @SysLog("应急响应中删除物资")
    public Result delete(int id) {
        Result result = new Result();
        try {
            emgPlanPublicGoodsService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @Override
    @RequestMapping(value = "/find", method = {RequestMethod.POST})
    public Result find(EmgPlanPublicGoods param, Page page) {
        Result result = new Result();
        try {
            result = emgPlanPublicGoodsService.find(param, page);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }


    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    @SysLog("应急响应中添加物资")
    public Result add(EmgPlanPublicGoods emgPlanPublicGoods) {
        Result result = new Result();
        try {
            result = emgPlanPublicGoodsService.add(emgPlanPublicGoods);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @GetMapping(value = "/planPublicGoodsHistory/{emgPlanPublicId}")
    public Result findHistory(@PathVariable("emgPlanPublicId") int emgPlanPublicId) {
        Result result = new Result();
        EmgPlanPublicGoodsHistory emgPlanPublicGoodsHistory = new EmgPlanPublicGoodsHistory();
        emgPlanPublicGoodsHistory.setEmgPlanPublicId(emgPlanPublicId);
        List<EmgPlanPublicGoodsHistory> emgPlanPublicGoodsHistories =
                emgPlanPublicGoodsHistoryMapper.select(emgPlanPublicGoodsHistory);
        if(CollectionUtil.isNotEmpty(emgPlanPublicGoodsHistories)){
            result.setData(emgPlanPublicGoodsHistories);
        }else{
            result.setData(new ArrayList<>());
        }
        return result;
    }
}
